<?php
$page = 1;
if(isset($_GET['page'])) {
	$page = $_GET['page'];
}

$type = "";
if(isset($_GET['type'])) {
	$type = $_GET['type'];
}

$arrcountrows = getRowsNews($type);
$sumrecord = $arrcountrows[0][0];
$numrecord = 20;
$pageNumber = intval(($sumrecord / $numrecord));
if(($sumrecord % $numrecord) != 0) {
	$pageNumber++;
}

if($pageNumber == 0) {
	$pageNumber = 1;	
}

if($page > $pageNumber) {
	$page = $pageNumber;
}

$ListView = getNewsLimit($type, ($page-1)*$numrecord, $numrecord);

$ListCateNew = getTypeNews();
?>
<div class="album_content">
	<div>
    	Loại Tin Tức: 
        <select class="typeduan">
        	<option value="">Hiển thị tất cả</option>
            <?php
			foreach($ListCateNew as $entry) {
			?>
            <option value="<?php echo($entry[0]); ?>" <?php if ($entry[0] == $type) { echo(" selected='selected'"); } ?>><?php echo($entry[1]); ?></option>
            <?php	
			}
			?>
        </select>
        <input class="btn_search" type="button" value="Hiển Thị" style="width: 80px; height: 25px;" />
    </div>
    <div style="clear:both;"></div>
	<form action="control/AdminNews.php" method="post" id="form_manager_action">
        <div class="the-buttom-edit-manager">
            <input type="hidden" name="act" id="id_action" value=""/><img src="../images/lockicon.png" width="25px" class="class-image-clock-buttom" />&nbsp;&nbsp;<img src="../images/unclockicon.png" width="25px" class="class-image-unclock-buttom" />&nbsp;&nbsp;<img src="../images/icon-delete.gif" width="25px" class="class-image-delete-buttom" />
        </div>
        <div style="clear:both; height:15px;"></div>
        <div class="class-pagination">
            <span class="class-pageLabel">Trang</span>
            <?php if($page == 1) { ?>
            <span title="Trang 1" page="1" class="class-pageNow">1</span>
            <?php } else { ?>
            <span title="Trang 1" page="1" class="class-pageElem">1</span>
            <?php }?>
            <?php if ($pageNumber != 1) { for($i = 2; $i <= $pageNumber; $i++) { ?>
            <?php if($i == $page) { ?>
            <span title="Trang <?php echo($i) ?>" page="<?php echo($i) ?>" class="class-pageNow"><?php echo($i) ?></span>
            <?php } else { ?>
            <span title="Trang <?php echo($i) ?>" page="<?php echo($i) ?>" class="class-pageElem"><?php echo($i) ?></span>
            <?php }?>
            <?php }}?>
        </div>
        <div style="clear:both; height:10px;"></div>
        <div style="min-width: 100%; overflow-x: scroll;">
            <table class="table-view-manager" cellspacing="1" cellpadding="0" border="0" width="2780px">
                <tr>
                    <th class="title" width="25px"></th>
                    <th class="title" width="35px">STT</th>
                    <th class="title" width="50px">Display</th>
                    <th class="title" width="50px">Edit</th>
                    <th class="title" width="50px">Delete</th>
                    <th class="title" width="450px">Title Dự Án</th>
                    <th class="title" width="80px">Hình Bìa</th>
                    <th class="title" width="150px">Tác Giả</th>
                    <th class="title">Nội Dung Tóm Tắt</th>
                    <th class="title" width="120px">Ngày tạo</th>
                    <th class="title" width="120px">Ngày cập nhật</th>
                </tr>
                <?php
                $i = ($page-1)*$numrecord + 1;
                foreach($ListView as $entry) {
                ?>
                <tr>
                    <td align="center"><input type="checkbox" name="idcheckmanager[]" class="idcheckmanager" value="<?php echo($entry[0]) ?>" /></td>
                    <td align="center"><?php echo($i) ?></td>
                    <td align="center"><?php if($entry[9] == 1) { ?><img src="../images/unclockicon.png" width="20px" title="Click để không cho phép hiển thị" style="cursor: pointer;" class="class-display-link" idpr="<?php echo($entry[0]) ?>" /><?php } else { ?><img class="class-hiden-link" idpr="<?php echo($entry[0]) ?>" src="../images/lockicon.png" width="20px" style="cursor: pointer;" title="Click để hiển thị trên trang chủ" /><?php } ?></td>
                    <td align="center"><a href="admin.php?c=new&amp;p=edit-news&amp;id=<?php echo($entry[0]) ?>"><img src="../images/icon-edit.gif" width="20px" style="cursor: pointer;" title="Chỉnh sửa thông tin" /></a></td>
                    <td align="center"><img src="../images/icon-delete.gif" width="20px" style="cursor: pointer;" idpr="<?php echo($entry[0]) ?>" class="class-deletequestion-link" title="Xóa" /></td>
                    <td><?php echo($entry[3]) ?></td>
                    <td><img src="<?php echo($entry[4]) ?>" width="80px" /></td>
                    <td align="center"><?php echo($entry[6]) ?></td>
                    <td><?php echo($entry[5]) ?></td>
                    <td align="center"><?php echo(int_to_date($entry[7])) ?></td>
                    <td align="center"><?php echo(int_to_date($entry[8])) ?></td>
                </tr>
                <?php
                $i++;
                }
                ?>
            </table>
        </div>
        <div style="clear:both; height:15px;"></div>
        <div class="class-pagination">
            <span class="class-pageLabel">Trang</span>
            <?php if($page == 1) { ?>
            <span title="Trang 1" page="1" class="class-pageNow">1</span>
            <?php } else { ?>
            <span title="Trang 1" page="1" class="class-pageElem">1</span>
            <?php }?>
            <?php if ($pageNumber != 1) { for($i = 2; $i <= $pageNumber; $i++) { ?>
            <?php if($i == $page) { ?>
            <span title="Trang <?php echo($i) ?>" page="<?php echo($i) ?>" class="class-pageNow"><?php echo($i) ?></span>
            <?php } else { ?>
            <span title="Trang <?php echo($i) ?>" page="<?php echo($i) ?>" class="class-pageElem"><?php echo($i) ?></span>
            <?php }?>
            <?php }}?>
        </div>
        <div style="clear:both; height:10px;"></div>
    </form>
</div>
<script>
	$('.btn_search').click(function(e) {		
		openwindowns('admin.php?c=new&p=ds-news&type=' + $('.typeduan').val() + '&page=' + <?php echo($page); ?>);
    });



	$(document).on('click', '.class-image-clock-buttom', function() {
		if($('.idcheckmanager:checked').length != 0) submitformmanager('clock');
		else alert('Vui lòng chọn danh sách cần cập nhật');
	});
	
	$(document).on('click', '.class-image-unclock-buttom', function() {
		if($('.idcheckmanager:checked').length != 0) submitformmanager('unclock');
		else alert('Vui lòng chọn danh sách cần cập nhật');
	});
	
	$(document).on('click', '.class-image-delete-buttom', function() {
		if($('.idcheckmanager:checked').length != 0) {
			if(confirm('Bạn thực sự muốn xóa danh sách đã chọn?')) { submitformmanager('del'); }
		} else 
			alert('Vui lòng chọn danh sách cần xóa');
	});
	
	function submitformmanager(value) {
		$('#id_action').val(value);
		$('#form_manager_action').submit();
	}
	
	$(document).on('click', '.class-pageElem', function() {
		page = $(this).attr('page');
		
		openwindowns('admin.php?c=new&p=ds-news&type=' + $('.typeduan').val() + '&page=' + page);
	});
	
	function openwindowns(url) {
		window.location = url;
	}
	
	$(document).on('click', '.class-display-link', function() {
		elem = $(this);
		id = elem.attr('idpr');
		$.ajax({
			type: "POST",
			url: "control/ajax/AjaxAdminNews.php",
			data:{act: 'edita',type: "0", idpe: id},
			dataType: "json",
			success: function(response){
				if(response == true) {
					elem.parent().html('<img class="class-hiden-link" idpr="' + id + '" src="../images/lockicon.png" width="20px" style="cursor: pointer;" title="Click để hiển thị trên trang chủ" />').hide().fadeIn(700);
				} else {
					alert("Cập nhật thất bại");
				}
			}
		});
	});
	
	$(document).on('click', '.class-hiden-link', function() {
		elem = $(this);
		id = elem.attr('idpr');
		$.ajax({
			type: "POST",
			url: "control/ajax/AjaxAdminNews.php",
			data:{act: 'edita',type: "1", idpe: id},
			dataType: "json",
			success: function(response){
				if(response == true) {
					elem.parent().html('<img src="../images/unclockicon.png" width="20px" title="Click để không cho phép hiển thị" style="cursor: pointer;" class="class-display-link" idpr="' + id + '" />').hide().fadeIn(700);
				} else {
					alert("Cập nhật thất bại");
				}
			}
		});
	});
	
	$(document).on('click', '.class-deletequestion-link', function() {
		elem = $(this);
		id = elem.attr('idpr');
		if(confirm('Bạn thực sự muốn xóa?')) {
			$.ajax({
				type: "POST",
				url: "control/ajax/AjaxAdminNews.php",
				data:{act: 'del', idpe: id},
				dataType: "json",
				success: function(response){
					if(response == true) {
						elem.parent().parent().fadeOut(1000, function() {$(this).remove()});
					} else {
						alert("Cập nhật thất bại");
					}
				}
			});
		}
	});
</script>